package com.a214.yxz.yuejian013.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.a214.yxz.yuejian013.JavaBean.ClubcourseBean;
import com.a214.yxz.yuejian013.R;

import java.util.List;

/**
 * Created by Oliver on 10/30/2017.
 */

public class ClubCourseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private LayoutInflater inflater;
    private List<ClubcourseBean> list;
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;


    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    public ClubCourseAdapter(Context context) {
        this.inflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = inflater.inflate(R.layout.club_course_item, parent, false);
        RecyclerView.ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ClubcourseBean bean = list.get(position);
        if (bean == null) return;
        final ViewHolder viewholder = (ViewHolder) holder;
        viewholder.intro.setText(bean.getIntroduction());
        viewholder.name.setText(bean.getCourse_name());
        viewholder.price.setText(bean.getCourse_price()+"元");
        if (mOnItemClickListener != null) {
            viewholder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = viewholder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(viewholder.itemView, position);
                }
            });
            viewholder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    int position = viewholder.getLayoutPosition();
                    mOnItemClickListener.onItemLongClick(viewholder.itemView, position);
                    return false;
                }
            });
        }
    }

    public void setList(List<ClubcourseBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (list == null) return 0;
        return list.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    private class ViewHolder extends RecyclerView.ViewHolder {
        TextView intro, name,price;

        public ViewHolder(View itemView) {
            super(itemView);
            intro = (TextView) itemView.findViewById(R.id.club_course_list_courseIntro);
            name = (TextView) itemView.findViewById(R.id.club_course_list_courseName);
            price = (TextView) itemView.findViewById(R.id.club_course_list_coursePrice);
        }

    }
}
